$(document).ready(function() {

	var navbar = $('#navbar'),
		sidebar = $('#sidebar'),
		mainContainer = $('#main-container');
	
	// Document events
	$(document).on('click', 'a.brand', function(){
		window.location.href = '/';
	});
	
	// Navbar events
	navbar.on('click', 'ul.nav > li', function(){
		$this = $(this);
		$this.parent().find('li.active').removeClass('active');
		$this.addClass('active');
		if($this.data('url') !== undefined){
			$.get($this.data('url'), function(data) {
				if ($this.hasClass('no-sidebar')){
					sidebar.closest('div.span3').addClass('hide');
					mainContainer.removeClass('span9').html(data);
				}
				else{
					sidebar.closest('div.span3').removeClass('hide').end().html(data).find('a').first().click();
					mainContainer.addClass('span9');
				}
			});
		}
	});

	// Sidebar menu events
	sidebar.on('click', 'a', function() {
		var $this = $(this);
		sidebar.find('li.active').removeClass('active');
		$this.parent().addClass('active');
		$.get($this.data('url'), function(data) {
			mainContainer.html(data);
		});
	});

	// Main container Pictures
	mainContainer.on('click', '#submit-picture', function(e) {
		e.preventDefault();
		$(this).after(getLoadingGif());
		$.ajax({
			type : 'POST',
			url : '/User/Picture/UploadPicture',
			enctype : 'multipart/form-data',
			data : new FormData($('form')[0]),
			contentType : false,
			processData : false,
			success : function(data) {
				submitCallback(data);
			}
		});
	}).on('click', '#submit-event', function(e) {
		e.preventDefault();
		$.ajax({
			type : 'POST',
			url : '/User/Event/AddEvent',
			data : new FormData($('form')[0]),
			contentType : false,
			processData : false,
			success : function(data) {
				submitCallback(data);
			}
		});
	}).on('click', '#submit-place', function(e) {
		e.preventDefault();
		$.ajax({
			type : 'POST',
			url : '/User/Place/AddPlace',
			data : new FormData($('form')[0]),
			contentType : false,
			processData : false,
			success : function(data) {
				submitCallback(data);
			}
		});
	}).on('click', '#submit-user', function(e) {
		e.preventDefault();
		$.ajax({
			type : 'POST',
			url : '/User/User/AddUser',
			data : new FormData($('form')[0]),
			contentType : false,
			processData : false,
			success : function(data) {
				submitCallback(data);
			}
		});
	}).on('click', '#place-search', function(){
		var address = $('#place-search-address').val();
		app.geocoder.geocode( { 'address': address }, function(results) {
			var location = results[0].geometry.location;
			addGmapMarker(location);
	        $('#address').val(results[0].formatted_address);
	        $('#latitude').val(location.lat());
	        $('#longitude').val(location.lng());
	    });
	}).on('click', 'button.delete-model', function(){
		$this = $(this);
		$this.prop('disabled', true).next().html(getLoadingGif());
		$.ajax({
		    url: $this.data('url'),
		    type: 'DELETE',
		    success: function(data) {
		    	if(data.error !== undefined){
		    		$this.prop('disabled', false).next().html(data.error);
		    	} else {
		    		$this.closest('tr').remove();
		    	}
		    }
		});
	}).on('click', 'button.edit-model', function(){
		$.get($(this).data('url'), function(data) {
			mainContainer.html(data);
		});
	}).on('click', '#change-password', function(e){
		e.preventDefault();
		$.ajax({
			type : 'POST',
			url : '/User/User/ChangePassword',
			data : new FormData($('form')[0]),
			contentType : false,
			processData : false,
			success : function(data) {
				mainContainer.html(data);
			}
		});
	});
	
	// Settings
	$('#settings').click(function(){
		sidebar.closest('div.span3').addClass('hide');
		$.get('/User/User/Settings', function(data) {
			mainContainer.html(data);
		});
	});
	
	
	// Logout
	$('#logout').click(function(){
		window.location.href = '/User/Logout';
	});

	// Load first menu's element
	navbar.find('ul.nav > li:first').click();
	
});